package cn.bluedot.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.bluedot.bean.Message;
import cn.bluedot.model.OutgoingRecord;
import cn.bluedot.model.Res;
import cn.bluedot.model.ResType;
import cn.bluedot.model.StorageRecord;
import cn.bluedot.model.StorageRecordVo;
import cn.bluedot.model.User;
import cn.bluedot.service.ResService;
import cn.bluedot.valid.StorageRecordVG;

@Controller
public class ResController {
    @Autowired
    private ResService resService;
    /**
     * 获取所有物品类型
     * @return
     */
    @RequestMapping("/resTypes")
    @ResponseBody
    public List<ResType> getAllResType() {
        return resService.getAllResType();
    }
    /**
     * 根据ID获取该种类的所有物品
     * @param resTypeID
     * @return
     */
    @RequestMapping("/resList")
    @ResponseBody
    public List<Res> getAllResByType(@RequestParam(value="resTypeID", required=true) long resTypeID){
        return resService.getAllResByTypeID(resTypeID);
    }
    
    /**
     * 提交入库记录
     resId=2&price=10&inNumber=100&standard=kg&totalPrice=1000&inPersonId=1&inTime=2019-3-11 00:00:00&curNumber=100&mtime=2019-3-11 00:00:00
     * @param storageRecord
     * @return
     */
    @RequestMapping("/addStorageRecord")
    @ResponseBody
    public Message addStorageRecord(@Validated(value = {StorageRecordVG.class}) StorageRecord storageRecord, 
            Errors errors) {
        Message msg = new Message(1, "入库成功");
        if (errors.hasErrors()) {
            msg.setState(0);
            msg.setMsg(errors.getFieldErrors().get(0).getDefaultMessage());
        } else {
            try {
                resService.warehousing(storageRecord);
            } catch (Exception e) {
                msg.setState(0);
                msg.setMsg("入库失败");
            }
        }
        return msg;
    }
    /**
     * 加载 入库记录
     * @param pageIndex
     * @return
     <th class="table-id">ID</th>
    <th class="table-title">物品名称</th>
    <th class="table-type">物品类别</th>
    <th class="table-main">单价</th>
    <th class="table-main">规格</th>
    <th class="table-author am-hide-sm-only">入库人</th>
    <th class="table-date">最后出库时间</th>
    <th class="table-main">当前数量/入库数量</th>
    <th class="table-set">操作</th>
     */
    @RequestMapping("/storageRecordList")
    @ResponseBody
    public List<StorageRecordVo> storageRecordList(@RequestParam(value="pageIndex", defaultValue="1") int pageIndex) {
        List<StorageRecordVo> showresList = resService.getListStorageRecordVO(pageIndex);
        for (StorageRecordVo srv : showresList) {
            System.out.println(srv);
        }
        return showresList;
    }
    
    /**
     * 加载该入库记录 的所有出库记录
     /outRecordByInId?storageRecordID=1
     */
    @RequestMapping("/outRecordByInId")
    @ResponseBody
    public List<OutgoingRecord> outRecordList(@RequestParam(value="pageIndex", defaultValue="1") int pageIndex, 
             @RequestParam(value="storageRecordID") long id) {
        return resService.getListOutgoingByStorageRecordId(id);
    }
    /**
     * 根据iD加载入库记录, 在出库时需要
     /getStorageRecord?storageRecordId=2
     * @param id
     * @return
     */
    @RequestMapping("/getStorageRecord")
    @ResponseBody
    public StorageRecord getStorageRecordById(@RequestParam(value="storageRecordId") Long id) {
        return resService.getStorageById(id);
    }
    /**
     * 对该入库记录, 进行出库提交. .. .
     * @return
     /submitOutRes?storageRecordId=1&outNumber=10&outPersonId=1
     private Long storageRecordId;
     private BigDecimal outNumber;
     private Long outPersonId;
     private Date outTime;
     private Date mtime;
     */
    @RequestMapping("/submitOutRes")
    @ResponseBody
    public Message StorageRecordOut(@Valid OutgoingRecord outgoingRecord, HttpSession session) {
        Message msg = new Message(1, "出库成功");
        System.out.println(outgoingRecord);
        StorageRecord sr = resService.getStorageById(outgoingRecord.getStorageRecordId());
        if (sr == null) {
            msg.setState(0);
            msg.setMsg("不存的入库记录");
        } else {
            // 当前库存量 < 出库数量
            if (sr.getCurNumber().compareTo(outgoingRecord.getOutNumber()) < 0) {
                msg.setState(0);
                msg.setMsg("库存量不足");
            } else {
                // 出库
                User u = (User) session.getAttribute("user");
                Date date = new Date();
                outgoingRecord.setMtime(date);
                outgoingRecord.setOutTime(date);
                outgoingRecord.setOutPersonId(1L); 
                outgoingRecord.setOutPersonId(u.getId());
                resService.addOutGoing(outgoingRecord);
            }
        }
        return msg;
    }
    
}
